<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addFatherPermission">添加权限</el-button>

      <el-table :data="list" default-expand-all row-key="id">
        <el-table-column label="名称" prop="name" row-key="id" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.pid === 0" size="mini" type="text" @click="addSonPermission(row.id)">添加</el-button>

            <el-button size="mini" type="text" @click="getPermissionDetail(row.id)">编辑</el-button>

            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>

        </el-table-column>

      </el-table>

    </div>
    <el-dialog title="新增权限点" :visible.sync="isShowDialog">
      <!-- 表单 -->
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>

        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>

        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>

        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>

      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="addPermission">确定</el-button>

          <el-button size="small" @click="closeDialog">取消</el-button>

        </el-col>
      </el-row>
    </el-dialog>

  </div>

</template>

<script>
import { addPermissionAPI, delPermissionAPI, editPermissionAPI, getPermissionDetailAPI, getPermissionListAPI } from '@/api/permission'
import { translateListToTreeData } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      isShowDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = translateListToTreeData(await getPermissionListAPI(), 0)
    },
    addPermission() {
      this.$refs.perForm.validate(async(valid) => {
        if (valid) {
          if (this.formData.id) {
            await editPermissionAPI(this.formData)
          } else {
            await addPermissionAPI(this.formData)
          }

          // 刷新列表
          this.getPermissionList()
          // 关闭弹窗
          this.closeDialog()
          this.$message.success('添加权限成功')
        }
      })
    },
    addSonPermission(id) {
      this.formData.pid = id
      this.formData.type = '2'
      this.isShowDialog = true
    },
    addFatherPermission() {
      this.formData.type = '1'
      this.formData.pid = '0'
      this.isShowDialog = true
    },
    closeDialog() {
      // 重置表单
      this.$refs.perForm.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      }
      // 关闭弹窗
      this.isShowDialog = false
    },
    // 修改表单
    async getPermissionDetail(id) {
      this.isShowDialog = true
      const res = await getPermissionDetailAPI(id)
      this.formData = res
    },
    // 删除操作
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await delPermissionAPI(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style>
.btn-add {
  margin: 10px;
}
</style>
